<?php

require_once '../include/common.inc.php';

if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

$menu_index = 2;
$page_title = 'JANSEN';
$guide_message = 'Customer | Order';

getMyParam('action');
getMyParam('default');
getMyParam('start');
getMyParam('end');

if ($default) {
    $action = 'search';
    $start = date('Y-m-d', $timestamp-3600*24*7);
    $end = date('Y-m-d', $timestamp);
}

$key = "Enter username...";
$user = isset($_POST['key']) && $_POST['key'] != $key ? $_POST['key'] : '';
$key = !isset($_POST['key']) ? $key : ($_POST['key'] != $key ? $_POST['key'] : '');


$unix_start = getUnixTime2($start);
$unix_end = getUnixTime2($end);

$sql_where = ' WHERE 1=1';
$no_user = false;

if ($action == "search" && (trim($key)  != "" || ($unix_start && $unix_end))) {
    if (trim($key)  != "") {
        $sql = "SELECT `id` FROM `user` WHERE `username` LIKE '%$key%'";
        $query = $db->query($sql);
        while($rs = $db->fetch_array($query)) {
            //$out_ary[] = $rs;
            $user_id_ary[] = $rs['id'];
        }

        if (!empty($user_id_ary)) {
            $user_id_str = implode(",", $user_id_ary);
            $sql_where .= " AND `user_id` IN (" . $user_id_str . ")";
        } else {
            $no_user = true;
        }
    }
    if ($unix_start && $unix_end) {
        //$sql_where .= " AND (`date`>$unix_start OR `date`>$unix_start) AND (`date` < $unix_end OR `date` = $unix_end)";
        $sql_where .= " AND `date`>$unix_start AND `date` < $unix_end";
    }
}

$user_info_ary = array();

if (true) {
    $page_url = 'c_order_list.php?';
    $ppp = 20;

    $sql = "SELECT `id` FROM `order`" . $sql_where;
    $query = $db->query($sql);
    $sql_all_num = $db->num_rows($query);

    // *** page code start ***
    getMyParam('page');
    $page = empty($page) || !ispage($page) ? 1 : $page;
    $start_limit = ($page - 1) * $ppp;
    if($start_limit > $sql_all_num) {
        $start_limit = 0;
        $page = 1;
    }
    // *** page code end ***


    $sql = "SELECT `id`, `product_info`, `totalcount`, `totalcost`, `user_id`, `date` FROM `order` " . $sql_where . " ORDER BY `date` DESC LIMIT $start_limit, $ppp";
    //echo $sql;
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $out_ary[] = $rs;
        $user_id_ary[] = $rs['user_id'];
    }

    $user_info_ary = array();
    if (!empty($user_id_ary)) {
        $user_id_str = implode(",", $user_id_ary);
        $sql = "SELECT `id`, `username`, `firstname`, `lastname`, `type` FROM `user` WHERE `id` IN (" . $user_id_str . ")";
        $query = $db->query($sql);
        while($rs = $db->fetch_array($query)) {
            $user_info_ary[$rs['id']] = $rs;
            $user_info_ary[$rs['id']]['money_type'] = $rs['type'] == 1 ? "HKD" : "USD";
        }
    }

    $page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);
}

include_once(JANSEN_ROOT . 'admin/menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_c_order_list');
require_once PrintEot('a_footer');

?>